﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Yes.Application.Comment.Dto;
using Yes.Core.Extensions;
using Yes.Core.Helper;
using Yes.Core.Pager;
using Yes.Domain;
using Yes.Domain.Context;
using Yes.Domain.Entity;
using Yes.Domain.Enum;

namespace Yes.Application.Comment
{
    public class CommentService : BaseService, ICommentService
    {
        private IRepository<CommentEntity> _commentRepository;
        private IRepository<ArticleEntity> _articleRepository;

        public CommentService(IRepository<CommentEntity> commentRepository, IRepository<ArticleEntity> articleRepository)
        {
            _commentRepository = commentRepository;
            _articleRepository = articleRepository;
        }

        public ResultDto Add(CommentAddDto dto, LoginInfo loginInfo)
        {
            if (dto.ArticleId <= 0)
            {
                return new ResultDto
                {
                    Status = false,
                    ErrorMessage = "文章不存在"
                };
            }

            var article = _articleRepository.Get(dto.ArticleId);
            if (article == null)
            {
                return new ResultDto
                {
                    Status = false,
                    ErrorMessage = "文章不存在"
                };
            }

            if (!article.CanComment)
            {
                return new ResultDto
                {
                    Status = false,
                    ErrorMessage = "文章已关闭评论"
                };
            }

            if (loginInfo != null)
            {
                dto.UserId = loginInfo.Id;
            }


            if (string.IsNullOrEmpty(dto.NickName))
            {
                return new ResultDto
                {
                    Status = false,
                    ErrorMessage = "请输入昵称"
                };
            }

            var comment = dto.MapTo<CommentEntity>();
            comment.Ip = "";//  RequestHelper.GetIp();
            comment.PostDate = DateTime.Now;
            comment.UserAgent = "";// HttpContext.Current.Request.UserAgent;
            comment.Status = CommentStatusEnum.Pending;
            _commentRepository.Insert(comment);
            /*
            var commentCount = _commentRepository.Count(x => x.ArticleId == article.Id&& x.Status== CommentStatusEnum.Normal);

            article.CommentCount = commentCount;
            _articleRepository.Update(article);
            */

            return new ResultDto
            {
                Status = true
            };
        }

        public PageInfo<CommentDto> GetPageInfo(CommentPagerDto dto)
        {
            var commentQuery = _commentRepository.TableNoTracking;
            if (!string.IsNullOrEmpty(dto.Keyword))
            {
                commentQuery = commentQuery.Where(p => p.Content.Contains(dto.Keyword));
            }
            if (dto.ArticleId > 0)
            {
                commentQuery = commentQuery.Where(p => p.ArticleId == dto.ArticleId);
            }


            commentQuery = commentQuery.Where(p => p.Status== dto.Status);

            var query = from comment in commentQuery
                        join article in _articleRepository.TableNoTracking.DefaultIfEmpty()
                    on comment.ArticleId equals article.Id into articleTemp
                        from article1 in articleTemp.DefaultIfEmpty()
                        select new CommentDto()
                        {
                            Id = comment.Id,
                            Title = article1.Title,
                            Content = comment.Content,
                            PostDate = comment.PostDate,
                            ArticleId = comment.ArticleId,
                            Url = comment.Url,
                            NickName = comment.NickName,
                            Email = comment.Email
                        };

            return query.OrderByDescending(p => p.Id).Paged(dto.PageIndex, dto.PageSize, query.Count());

        }


        public ResultDto Update(List<int> ids, string action)
        {
            if (ids.Count == 0)
            {
                return new ResultDto
                {
                    Status = false,
                    ErrorMessage = "请选择要处理的评论"
                };
            }

            switch (action)
            {
                case "Recovery":
                    _commentRepository.Update(x => ids.Contains(x.Id), p => new CommentEntity() { Status = CommentStatusEnum.Pending });
                    break;
                case "Check":
                    _commentRepository.Update(x => ids.Contains(x.Id), p => new CommentEntity() { Status = CommentStatusEnum.Normal });
                    break;
                case "UnCheck":
                    _commentRepository.Update(x => ids.Contains(x.Id), p => new CommentEntity() { Status = CommentStatusEnum.Deleted });
                    break;
                case "Del":
                    _commentRepository.Delete(x => ids.Contains(x.Id));
                    break;

            }

            var articleIdList = _commentRepository.Query(x => ids.Contains(x.Id)).Select(p=>p.ArticleId).ToList();

            foreach (var id in articleIdList)
            {
                var commentCount = _commentRepository.Count(x => x.ArticleId == id && x.Status == CommentStatusEnum.Normal);

                var article = _articleRepository.Query(x => x.Id == id).FirstOrDefault();
                if (article != null)
                {
                    article.CommentCount = commentCount;
                    _articleRepository.Update(article);
                }
            }

            return new ResultDto
            {
                Status = true
            };
        }
    }
}
